#include "caculate.h"
#include "math.h"
#include "def.h"
double caculate_expression(char* strSuffixExpression,double num[],int n)
{
	double dNumStack[MAX_NUM] = {0};
	double tmpResult;
	char* p1;
	int i = 0;		//flag the num[]
	int iStackTop = 0;	//flag the numStack[]
	p1 = strSuffixExpression;
	while (*p1 != 0)
	{
		switch (*p1)	
		{
		case 'n':
			i++;
			iStackTop++;
			dNumStack[iStackTop] = num[i];
			break;
		case '+':
			tmpResult = dNumStack[iStackTop] + dNumStack[iStackTop-1];
			dNumStack[iStackTop] = 0;
			iStackTop--;
			dNumStack[iStackTop] = tmpResult;
			break;
		case '-':
			tmpResult = dNumStack[iStackTop-1] - dNumStack[iStackTop];
			dNumStack[iStackTop] = 0;
			iStackTop--;
			dNumStack[iStackTop] = tmpResult;
			break;
		case '*':
			tmpResult = dNumStack[iStackTop-1] * dNumStack[iStackTop];
			dNumStack[iStackTop] = 0;
			iStackTop--;
			dNumStack[iStackTop] = tmpResult;
			break;
		case '/':
			tmpResult = dNumStack[iStackTop-1] / dNumStack[iStackTop];
			dNumStack[iStackTop] = 0;
			iStackTop--;
			dNumStack[iStackTop] = tmpResult;
			break;
		case 'A': //sin
			tmpResult = 0;
			tmpResult = sine(&dNumStack[iStackTop]);
			break;
		case 'B': //cos
			tmpResult = 0;
			tmpResult = cos(&dNumStack[iStackTop]);
			break;
		case 'C': //ln
			tmpResult = 0;
			tmpResult = ln(&dNumStack[iStackTop]);
			break;
		case 'D': //exp
			tmpResult = 0;
			tmpResult = exp(&dNumStack[iStackTop]);
			break;
			//dNumStack[iStackTop] = tmpResult;
		case 'E': //sqrt
			tmpResult = 0;
			tmpResult = sqrt(&dNumStack[iStackTop]);
			break;
			//dNumStack[iStackTop] = tmpResult;
		case 'F': //power
			tmpResult = 0;
			tmpResult = power(&dNumStack[iStackTop-1],&dNumStack[iStackTop]);
			dNumStack[iStackTop] = 0;
			iStackTop--;
			break;
		case 'G': //fabs
			tmpResult = 0;
			tmpResult = fabs(&dNumStack[iStackTop]);
			break;
		default:
			break;	
		}
		p1++;
	}
	num[0] = dNumStack[1];
	return dNumStack[1];
}
